<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
    	$m = M("page");
    	$page = 1;
    	$page = $_GET["page"];
    	$count = $m->count();
    	$eachpage = 4;
    	if($page<1)
    		$page = 1;
    	else if($page>ceil($count/$eachpage))
    		$page = ceil($count/$eachpage);
    	$list = $m->limit($eachpage*($page-1),$eachpage)->select();
    	$this->assign('list',$list);
    	$this->assign('page',$page);
    	$this->assign('allpage',ceil($count/$eachpage));
    	
        $this->display();
    }
    public function detail(){
    	$ping = M();
    	$pingdec = M('ping');
    	$m = M("page");
    	$id = $_GET["id"];
    	$list = $m->where("id=".$id)->limit("1")->find();
    	$plist = $m->where("id<".$id)->limit("1")->find();
    	$nlist = $m->where("id>".$id)->limit("1")->find();
    	$pinglist = $ping->query("select * from ping a left join pingdec b on a.pid=b.pingid where pageid=".$id." order by b.pingid");
    	$pinglist1 = $pingdec->where("pageid=".$id)->select();
    	$this->assign('pinglist',$pinglist);
    	$this->assign('pinglist1',$pinglist1);
    	$this->assign('list',$list);
    	$this->assign('plist',$plist);
    	$this->assign('nlist',$nlist);
        $this->display("contect");
    }
    public function login(){
    	$name = $_POST['name'];
    	$pwd = $_POST['pwd'];
    	$m = M("user");
    	$list = $m->where("name=".$name." and pwd=".$pwd)->find();
    	if(!$list){
    		$this->ajaxReturn(array(
    			'status' => 0,
    			'mass' => "用户名或者密码错误"
    		));
    	}else{
    		$_SESSION['name']=$name;
    		$this->ajaxReturn(array(
    			'status' => 1,
    			'mass' => "可以！"
    		));
    	}
    }
    public function newlogin(){
    	$name = $_POST['name'];
    	$pwd = $_POST['pwd'];
    	$m = M("user");
    	$list = $m->where("name=".$name)->find();
    	if($list)
    		$this->ajaxReturn(array(
    			'status' => 0,
    			'mass' => "已存在用户名啊"
    		));
    	else{
    		$data = array('name' => $name,"pwd" =>$pwd );
	    	if($m->add($data)){
	    		$_SESSION['name']=$name;
	    		$this->ajaxReturn(array(
	    			'status' => 1,
	    			'mass' => "可以的！"
	    		));

	    	}else{
	    		$this->ajaxReturn(array(
	    			'status' => 0,
	    			'mass' => "错了啊"
	    		));
	    	}
    	}
    	
    }
    public function checklogin(){
    	if($_SESSION['name']){
    		$this->ajaxReturn(array(
    			'status' => 1,
    			'mass' => $_SESSION['name']
    		));
    	}else{
    		$this->ajaxReturn(array(
    			'status' => 0,
    			'mass' => '没登录'
    		));
    	}
    }
    public function makeping(){
        $data = array("pageid" => $_POST['id'],"name" => $_POST['user'],"dec" => $_POST['ping'],"time" => $_POST['time']);
        $m = M("ping");
        $M = M();
        if($m->add($data)){
            $list = $M->query("select max(pid) as pid from ping");
            $this->ajaxReturn(array(
                'status' => 1,
                'mass' => $list
            ));
        }else{
            $this->ajaxReturn(array(
                'status' => 0,
                'mass' => '出错'
            ));
        }
    }
    public function makepingdec(){
        $data = array("pingid" => $_POST['id'],"lname" => $_POST['user'],"ldec" => $_POST['ping'],"ltime" => $_POST['time']);
        $m = M("pingdec");
        if($m->add($data)){
            $this->ajaxReturn(array(
                'status' => 1,
                'mass' => "评论成功"
            ));
        }else{
            $this->ajaxReturn(array(
                'status' => 0,
                'mass' => '出错'
            ));
        }
    }
}